﻿//地图JS
var ISS_MAP = {
    version: '1.0'
};
//公共常量
ISS_MAP.nearByMap = '';

//有数据正在加载
ISS_MAP.Loading = false;

//查询内容
ISS_MAP.Keyword = "";

//语音按钮枚举
var ISS_VOICE_TYPE_ENUM = {
    //起点按钮
    start: "start",
    //终点按钮
    end: "end"
};

//语音按钮
ISS_MAP.VoiceBtn = ISS_VOICE_TYPE_ENUM.start;

//地图初始化
ISS_MAP.Init = function () {
    //定义地图
    ISS_MAP.nearByMap = new BMap.Map("map-box", { enableMapClick: false });
    //启用滚轮放大缩
    ISS_MAP.nearByMap.enableScrollWheelZoom();
    //定义地图风格
    ISS_MAP.nearByMap.setMapStyle({ style: ISS_MAP_TYPE });
    //添加缩放控件
    var navigationControl = new BMap.NavigationControl({
        //靠左上角位置
        anchor: BMAP_ANCHOR_BOTTOM_RIGHT,
        //LARGE类型
        type: BMAP_NAVIGATION_CONTROL_ZOOM
    });
    ISS_MAP.nearByMap.addControl(navigationControl);
    // 添加定位控件
    var geolocationControl = new BMap.GeolocationControl();
    geolocationControl.addEventListener("locationSuccess", function (e) {
        // 定位成功事件
        ISS_MAP.LocationSuccess(e.point, 'to');
    });
    geolocationControl.addEventListener("locationError", function (e) {
        //定位失败事件
        ISS_MAP.LocationError();
    });
    ISS_MAP.nearByMap.addControl(geolocationControl);
    //绑定地理位置
    ISS_MAP_CENTER_NAME = decodeURI(ISS.utils.getParameter("name"));
    ISS_MAP_CENTER_LNG = ISS.utils.getParameter("lng");
    ISS_MAP_CENTER_LAT = ISS.utils.getParameter("lat");
    var point = new BMap.Point(ISS_MAP_CENTER_LNG, ISS_MAP_CENTER_LAT);
    ISS_MAP.nearByMap.centerAndZoom(point, 13);
    //标记位置
    var marker = new BMap.Marker(point);
    ISS_MAP.nearByMap.addOverlay(marker);
    var label = new BMap.Label(ISS_MAP_CENTER_NAME, { offset: new BMap.Size(20, -10) });
    marker.setLabel(label);
};

//设置地图中心店
ISS_MAP.SetCenter = function (lng, lat, zoom) {
    var point = new BMap.Point(lng, lat);
    ISS_MAP.nearByMap.centerAndZoom(point, zoom);
}

//百度地图坐标转换
ISS_MAP.TranPoint = function (position) {
    var currentLat = position.coords.latitude;
    var currentLon = position.coords.longitude;
    var gpsPoint = new BMap.Point(currentLon, currentLat);
    BMap.Convertor.translate(gpsPoint, 0, ISS_MAP.DrawTranPoint); //转换坐标 
}

// 定位成功事件
ISS_MAP.LocationSuccess = function (point, tofrom) {
    if ((tofrom == 'from' && $("#searchinput_end").val() == '')
    || (tofrom == 'to' && $("#searchinput_start").val() == '')) {
        //赋值当前点
        ISS_MAP_LOCAL_LNG = point.lng;
        ISS_MAP_LOCAL_LAT = point.lat;
        ISS_MAP_LOCAL_NAME = '我的位置';
        //赋值起始点
        if (tofrom == 'from') {
            ISS_MAP_SEARCH_END.Lng = point.lng;
            ISS_MAP_SEARCH_END.Lat = point.lat;
            ISS_MAP_SEARCH_END.Name = '我的位置';
            $("#searchinput_end").css("color", "#3385ff");
            $("#searchinput_end").val("我的位置");
        } else {
            ISS_MAP_SEARCH_START.Lng = point.lng;
            ISS_MAP_SEARCH_START.Lat = point.lat;
            ISS_MAP_SEARCH_START.Name = '我的位置';
            $("#searchinput_start").css("color", "#3385ff");
            $("#searchinput_start").val("我的位置");
        }
    }
};

// 定位失败事件
ISS_MAP.LocationError = function (tofrom) {
    if ((tofrom == 'from' && $("#searchinput_end").val() == '')
    || (tofrom == 'to' && $("#searchinput_start").val() == '')) {
        ISS_MAP_LOCAL_LNG = '';
        ISS_MAP_LOCAL_LAT = '';
        if (tofrom == 'from') {
            $("#searchinput_end").css("color", "#6C6C6C");
            $("#searchinput_end").val("获取位置失败");
        } else {
            $("#searchinput_start").css("color", "#6C6C6C");
            $("#searchinput_start").val("获取位置失败");
        }
    }
};

//绘制我的位置
ISS_MAP.DrawTranPoint = function (point) {
    //定位我的位置
    ISS_MAP.nearByMap.centerAndZoom(point, 15);
    ISS_MAP.nearByMap.addOverlay(new BMap.Marker(point))
}

//绘点
ISS_MAP.DrawArc = function (longitude, latitude, paramFlag, n) {
    var point = new BMap.Point(longitude, latitude);
    //图标
    var v_offset_y = (n - 1) * 50;
    var c_myIcon = "";
    if (paramFlag == 1) {
        c_myIcon = new BMap.Icon("/Common/website/assets/images/common/map/Base/ico-map.png", new BMap.Size(25, 42), {
            offset: new BMap.Size(25, 29),
            imageOffset: new BMap.Size(-30, 0 - v_offset_y)
        });
    } else if (paramFlag == 2) {
        c_myIcon = new BMap.Icon("/Common/website/assets/images/common/map/Base/ico-map.png", new BMap.Size(25, 42), {
            offset: new BMap.Size(25, 29),
            imageOffset: new BMap.Size(-90, 0 - v_offset_y)
        });
    } else if (paramFlag == 3) {
        c_myIcon = new BMap.Icon("/Common/website/assets/images/common/map/Base/ico-map.png", new BMap.Size(25, 42), {
            offset: new BMap.Size(25, 29),
            imageOffset: new BMap.Size(-150, 0 - v_offset_y)
        });
    }
    var marker = new BMap.Marker(point, { icon: c_myIcon }); // 创建点
    ISS_MAP.nearByMap.addOverlay(marker);
    ISS_MAP.nearByMap.setZoom(11);
}

//清除地图覆盖物
ISS_MAP.ClearArc = function () {
    ISS_MAP.nearByMap.clearOverlays();
}

//Pick Modal
ISS_MAP.PickModal = function (searchResult) {
    myApp.pickerModal(".picker-1");
    if (searchResult == true) {
        $("#pickbar")[0].style.height = "43%";
        $("#map-box")[0].style.height = "57%";
    } else {
        $("#pickbar")[0].style.height = "50px";
        $("#map-box")[0].style.height = "calc(100% - 50px)";
    }
}
ISS_MAP.CloseModal = function () {
    //myApp.closeModal(".picker-1");
    $("#pickbar")[0].style.height = "0%";
    $("#map-box")[0].style.bottom = "0%";
}

//初始化
ISS_MAP.Init();